﻿package com.msjt.dao;

import java.util.List;

import com.msjt.base.BaseDAO;
import com.msjt.entity.Question;

/**
 * name:QuestionDAO function:咨询模块的数据处理层 author: kw time:2013.8.27 updateTime:
 */
public class QuestionDAO extends BaseDAO {
	// 主页加载模块，5条信息
	@SuppressWarnings("unchecked")
	public List<Question> getIndexQuestion() {
		String hql = "select new Question(q.quesId,q.quesTitle) from Question as q order by q.quesDate desc";
		return (List<Question>) this.searchByPage(hql, null, 1, 5);
	}

	// 后台分页得到行业咨询
	@SuppressWarnings("unchecked")
	public List<Question> getQuestionByPageAfter(int pageIndex, int pageSize) {
		String hql = "select q from Question as q order by q.quesDate desc";
		return (List<Question>) this.searchByPage(hql, null, pageIndex,
				pageSize);
	}

	// 前台分页只加载ID，标题，问题内容，日期
	@SuppressWarnings("unchecked")
	public List<Question> getQuestionByPage(int pageIndex, int pageSize) {
		String hql = "select new Question(q.quesId,q.quesTitle,q.quesContent,q.quesDate) from Question as q  order by q.quesDate desc";
		return (List<Question>) this.searchByPage(hql, null, pageIndex,
				pageSize);
	}

	// 添加行业咨询
	public boolean addQuestion(Question question) {
		// insert into question(q_title,q_content,q_type,in_Id)
		// values('我是咨询5','为什么回答5','未审核',1)
		// String
		// hql="insert into Question(quesTitle,quesContent,quesType) values=(?,?,'未审核')";
		// String
		// sql="insert into question(q_title,q_content,q_type) values(?,?,'未审核');";
		// Object [] params={question.getQuesTitle(),question.getQuesContent()};
		// return this.updateBySql(sql, params);
		// String
		// hql="insert into Question as q values(q.qTitle,q.qContent,q.q_type)";
		return this.save(question);
	}

	// 回答咨询，回答完咨询后即变为关闭状态
	public boolean addAnswer(Question question) {
		String hql = "update Question as q set q.quesAnswer=?,q.quesType='关闭' where q.quesId=?";
		Object[] params = { question.getQuesAnswer(), question.getQuesId() };
		return this.updateByHql(hql, params);
	}

	// 管理咨询（删除）
	public boolean deleteQuestion(Question question) {
		return this.deleteById(question.getQuesId());
	}

	// 审核咨询信息
	public boolean updateState(Question question) {
		String queryString = "update Question as q set q.quesType=? where q.quesId=?";
		Object[] parameters = { question.getQuesType(), question.getQuesId() };
		return this.updateByHql(queryString, parameters);
	}

	// 根据ID得到一个咨询信息
	public Question getOneQuestion(Question question) {
		return (Question) this.findById(question.getQuesId());
	}

	// 修改问题答案
	public boolean updateAnswer(Question question) {
		String hql = "update Question as q set q.quesAnswer=? where q.quesId=?";
		Object[] parameters = { question.getQuesAnswer(), question.getQuesId() };
		return this.updateByHql(hql, parameters);
	}

	// 查看咨询个数
	public Integer getQuestionCount(Question question) {
		if (question == null) {
			String hql = "select count(ques.quesId) from Question as ques where 1=1";
			return this.getTotalCount(hql, null);
		}
		if (question.getQuesTitle() != null
				&& (!question.getQuesTitle().equals(""))
				&& (question.getQuesType() == null || question.getQuesType()
						.equals(""))) {
			String hql = "select count(ques.quesId) from Question as ques where ques.quesTitle like ?";
			Object[] params = { "%" + question.getQuesTitle() + "%" };
			return this.getTotalCount(hql, params);
		}
		if (question.getQuesType() != null
				&& (!question.getQuesType().equals(""))
				&& (question.getQuesTitle() == null || question.getQuesTitle()
						.equals(""))) {
			String hql = "select count(ques.quesId) from Question as ques where ques.quesType=?";
			if (question.getQuesType().equals("已回答")) {
				hql = "select count(ques.quesId) from Question as ques where ques.quesType is not null";
				return this.getTotalCount(hql, null);
			}
			Object[] params = { question.getQuesType() };
			return this.getTotalCount(hql, params);
		}
		if (question.getQuesType().equals("已回答")) {
			String hql = "select count(ques.quesId) from Question as ques where ques.quesTitle like ? and ques.quesType is not null";
			Object[] params = { "%" + question.getQuesTitle() + "%" };
			return this.getTotalCount(hql, params);
		}
		String hql = "select count(ques.quesId) from Question as ques where ques.quesTitle like ? and ques.quesType=?";
		Object[] params = { "%" + question.getQuesTitle() + "%",
				question.getQuesType() };
		return this.getTotalCount(hql, params);

	}

	// 模糊查询
	@SuppressWarnings("unchecked")
	public List<Question> selectByLike(Question question, int pageIndex,
			int pageSize) {
		if (question == null) {
			return this.getQuestionByPageAfter(pageIndex, pageSize);
		}
		if (question.getQuesTitle() != null
				&& (!question.getQuesTitle().equals(""))
				&& (question.getQuesType() == null || question.getQuesType()
						.equals(""))) {
			String hql = "select ques from Question as ques where ques.quesTitle like ? order by ques.quesDate desc";
			Object[] params = { "%" + question.getQuesTitle() + "%" };
			return (List<Question>) this.searchByPage(hql, params, pageIndex,
					pageSize);
		}
		if (question.getQuesType() != null
				&& (!question.getQuesType().equals(""))
				&& (question.getQuesTitle() == null || question.getQuesTitle()
						.equals(""))) {
			String hql = "select ques from Question as ques where ques.quesType=?   order by ques.quesDate desc";
			if (question.getQuesType().equals("已回答")) {
				hql = "select ques from Question as ques where ques.quesAnswer is not null order by ques.quesDate desc";
				return (List<Question>) this.searchByPage(hql, null, pageIndex,
						pageSize);
			}
			Object[] params = { question.getQuesType() };
			return (List<Question>) this.searchByPage(hql, params, pageIndex,
					pageSize);
		}
		if (question.getQuesType().equals("已回答")) {
			String hql = "select ques from Question as ques where ques.quesTitle like ? and ques.quesAnswer is not null order by ques.quesDate desc";
			Object[] params = { "%" + question.getQuesTitle() + "%" };
			return (List<Question>) this.searchByPage(hql, params, pageIndex,
					pageSize);
		}
		String hql = "select ques from Question as ques where ques.quesTitle like ? and ques.quesType=? order by ques.quesDate desc";
		Object[] params = { "%" + question.getQuesTitle() + "%",
				question.getQuesType() };
		return (List<Question>) this.searchByPage(hql, params, pageIndex,
				pageSize);
	}
	
	/**
	 * 批量删除
	 * @param idString id的字符串，用“,”号隔开
	 * @return
	 */
	public boolean deleteMore(String idString){
		String hql="delete Question as ques where ques.quesId in ("+idString+")";
		return this.updateByHql(hql, null);
	}
}
